<?php
require('../database/db.php');
$mysql=new mysql();
error_reporting(0);
// 分页
if($_REQUEST['page']!="" && $_REQUEST['pageSize']!=""){
	$de=$_REQUEST['page'] * $_REQUEST['pageSize'];
	$size=$_REQUEST['pageSize'];
}else{
	$de=0;
	$size=10;
}

// 分页 end
$homefenleiId=$_REQUEST[homefenleiId];
$cateid=$_REQUEST[cateid];
$cityid=$_REQUEST[cityid];
$isHome=$_REQUEST[isHome];
// 传入经纬度
$longitude=$_REQUEST[longitude];
$latitude=$_REQUEST[latitude];
// 传入经纬度 end

//一个月的时间
$t = getdate(time());
$sdate = $t[year].'-'.$t[mon].'-'.'01 00:00:00';
$edate = $t[year].'-'.$t[mon].'-'.'31 23:59:59';
// 一个月的时间 end

if (($homefenleiId!='' || $cateid!='') && $cityid!='' && $longitude!='' && $latitude!='') {
	if($isHome==1){
          $sql="select *,(SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),2) + POWER(ABS(latitude - {$latitude}),2))*160) AS distance from shop where city=".$cityid." and leimu in (".$cateid.") and `open`=1 and `check`=1 ORDER BY distance limit $de,$size";
	}elseif($isHome==2){
          $sql="select *,(SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),2) + POWER(ABS(latitude - {$latitude}),2))*160) AS distance from shop where city=".$cityid." and leimu=".$cateid." and `open`=1 and `check`=1 ORDER BY distance limit $de,$size";
          
	}
	
	$shop=$mysql->getAll($sql);

	$ids=count($shop);
	for($i=0;$i<$ids;$i++){
		$shopid=$shop[$i][shopid];
		$sql2= "select count(*) as num from goodsorders where addtime>='$sdate' and addtime<='$edate' and shopid=$shopid and state<>0 and state<>4";
		$result=$mysql->getAll($sql2);
		// $num[] = $result[0][num];

		$shop[$i][numbers] = $result[0][num];
		$sql4 = "select goodsid,price,images from goods where shopid = ".$shopid." limit 0,3";
		$result4 =$mysql->getAll($sql4);
		$shop[$i]['good_list'] = $result4;
	}

	$arr = array('code' => 200, 'msg' =>'查询成功' ,'data' =>$shop);
}else{
	$arr = array('code' => 300, 'msg' =>'参数传入有误,需传入分类id=>cateid,首页分页id=>homefenleiId(cateid,homefenleiId),区域id=>cityid,经度=>longitude,纬度=>latitude');
}
echo json_encode($arr);

?>